package org.jcms.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Admin;
import org.jcms.model.Customer;
import org.jcms.model.Manager;
import org.jcms.service.AdminServiceI;
import org.jcms.service.CustomerServiceI;
import org.jcms.service.ManagerServiceI;
import org.jcms.uitils.MD5;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 接口参数
 * 
 * username 用户名
 * 
 * password 密码
 * 
 * type 1：学生、教师 2：教材发行 3：仓库采购 4：管理员
 * 
 * verifycode 验证码
 * 
 * 
 * 返回值
 * 
 * err :错误标志 0为登陆成功 1为登陆失败
 * 
 * msg : 登陆成功返回登陆的用户类别代码 登陆失败返回原因
 * 
 * @author 赵泽洋
 * 
 */
public class Login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		AdminServiceI adminServiceI = (AdminServiceI) ac
				.getBean("adminServiceImpl");
		CustomerServiceI customerServiceI = (CustomerServiceI) ac
				.getBean("customerServiceImpl");
		ManagerServiceI managerServiceI = (ManagerServiceI) ac
				.getBean("managerServiceImpl");

		String retJson = "{\"error\":@err,\"msg\":\"@msg\"}";
		String err = "1";
		String msg = "";

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String type = request.getParameter("type");
		String verifycode = request.getParameter("verifycode");

		try {

			// 验证码验证
			if (verifycode == null
					|| !verifycode.equals(request.getSession().getAttribute(
							"validetecode"))) {
				msg = "验证码错误";
				return;

			}

			int iType = Integer.parseInt(type);
			switch (iType) {
			case 1:
				Customer customer = customerServiceI.findById(username);

				if (customer == null) {
					msg = "用户不存在";
					return;
				}

				if (customer.getCustomerIsBlocked() == 1) {
					msg = "帐号已经被停用";
					return;
				}

				if (!customer.getCustomerPassword().equals(MD5.md5s(password))) {
					msg = "密码错误!";
					return;
				}

				err = "0";
				msg = "1";
				request.getSession().setAttribute("customer", customer);

				break;

			case 2:
				Manager manager = managerServiceI.findById(username);

				if (manager == null) {
					msg = "用户不存在";
					return;
				}

				if (manager.getManagerType() != 0) {
					msg = "用户不存在";
					return;

				}

				if (manager.getManagerIsBlocked() == 1) {
					msg = "帐号已经被停用";
					return;
				}

				if (!MD5.md5s(password).equals(manager.getManagerPassword())) {
					msg = "密码错误!";
					return;
				}

				err = "0";
				msg = "2";
				request.getSession().setAttribute("manager-0", manager);
				break;
			case 3:
				Manager manager2 = managerServiceI.findById(username);

				if (manager2 == null) {
					msg = "用户不存在";
					return;
				}

				if (manager2.getManagerType() != 1) {
					msg = "用户不存在";
					return;

				}

				if (manager2.getManagerIsBlocked() == 1) {
					msg = "帐号已经被停用";
					return;
				}

				if (!MD5.md5s(password).equals(manager2.getManagerPassword())) {
					msg = "密码错误!";
					return;
				}

				err = "0";
				msg = "3";
				request.getSession().setAttribute("manager-1", manager2);

				break;
			case 4:
				Admin admin = adminServiceI.findById(username);
				if (admin == null) {
					msg = "用户不存在";
					return;
				}
				if (admin.getAdminIsBlocked() == 1) {
					msg = "帐号已经被停用";
					return;
				}
				if (!MD5.md5s(password).equals(admin.getAdminPassword())) {
					msg = "密码错误!";
					return;
				}

				err = "0";
				msg = "4";
				request.getSession().setAttribute("admin", admin);
				break;

			default:
				msg = "类别错误";
				break;
			}

		}

		catch (Exception e) {
			msg = "系统错误";
		}

		finally {
			// 用完清空验证码
			request.getSession().setAttribute("validetecode", null);
			retJson = retJson.replace("@err", err);
			retJson = retJson.replace("@msg", msg);
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
